﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Antioch.Base
{
	[Jemgine.Engine.Category("Antioch System"), Jemgine.Engine.Component]
	public class PlayerNavigationNode : Jemgine.Engine.EntityComponent, Jemgine.Data.Rotatable, Jemgine.Data.EditorDrawable
	{
        public float Angle { get; set; }

        [ContentSerializerIgnore]
        internal bool TouchedThisFrame = false;

        protected virtual void SetExtents(Jemgine.Math.OBB OBB) {}

        public Jemgine.Math.OBB GetWorldOBB()
        {
            var OBB = new Jemgine.Math.OBB();
            OBB.Angle = Me.Angle;
            OBB.Center = Me.TransformToWorldSpace(Position);
            SetExtents(OBB);
            return OBB;
        }

        public PlayerNavigationNode()
        {
            Angle = 0;
        }

        public override void _Initialize(ContentManager Content)
        {
            PlayerNavigationModule Module = Sim.GetModule<PlayerNavigationModule>();
            if (Module == null) return;
            Module.AddNode(this);
        }

        public override void _Destroy()
        {
            PlayerNavigationModule Module = Sim.GetModule<PlayerNavigationModule>();
            if (Module == null) return;
            Module.RemoveNode(this);
        }

        public Box2D.XNA.Body GetPhysicsBody()
        {
            Jemgine.Engine.Physics Physics = Me.GetComponent<Jemgine.Engine.Physics>();
            if (Physics != null) return Physics.PhysicsBody;
            else return null;
        }

        #region Rotatable Members

        object Jemgine.Data.Rotatable.PrepareToRotate()
        {
            return Angle;
        }

        void Jemgine.Data.Rotatable.ApplyRotation(object Tag, float Rotation)
        {
            float OriginalAngle = (float)Tag;
            Angle = OriginalAngle + Rotation;
        }

        void Jemgine.Data.Rotatable.Snap(Jemgine.Data.Snapper Snapper)
        {
            Angle = Snapper.SnapRotation(Angle);
        }

        #endregion

        #region EditorDrawable Members

        void Jemgine.Data.EditorDrawable.Draw(int ID, Jemgine.Data.EditorDrawData Context, Jemgine.Data.SelectionType Selected)
        {
            base.Draw(ID, Context, Selected);

            if (Selected == Jemgine.Data.SelectionType.Selected || Selected == Jemgine.Data.SelectionType.WorkingSet)
            {
                Context.Renderer.Color = Color.Gold.ToVector4();

                Context.Renderer.DrawLine(Position,
                    Position + Vector2.Transform(new Vector2(0, 2), Matrix.CreateRotationZ(Angle - (float)Math.PI / 4.0f)),
                    0.2f);
                Context.Renderer.DrawLine(Position,
                    Position + Vector2.Transform(new Vector2(0, 2), Matrix.CreateRotationZ(Angle + (float)Math.PI / 4.0f)),
                    0.2f);

            }
        }

        #endregion
    }

 
}
